//
//  BaseViewController.m
//  WeCommunity
//
//  Created by mac on 2017/6/14.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()<UIGestureRecognizerDelegate>

@property (nonatomic, assign)BOOL isCanUseSideBack;  // 手势是否启动

@end

@implementation BaseViewController
#pragma mark - get
-(UIBarButtonItem *)leftItem
{
    if (!_leftItem) {
        _leftItem = self.navigationItem.leftBarButtonItems[0];
    }
    return _leftItem;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //防止标题过长 把下级界面标题挤跑偏
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                             initWithTitle:@""
                                             style:UIBarButtonItemStylePlain
                                             target:self
                                             action:nil];
    
    UIView * navcView = [[UIView alloc] initWithFrame:CGRectMake(0, -KNaviheight, KScreen_Width, KNaviheight)];
    navcView.backgroundColor = NAVC_COLOR;
    [self.view addSubview:navcView];
}

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    //导航控制器的第一个视图控制器关闭边缘返回手势
    if (self.navigationController.viewControllers.count == 1) {
        [self forbiddenSideBack];
    }else if (self.navigationController.viewControllers.count > 1){
        //恢复边缘返回手势
        [self resetSideBack];
    }
}

//一般不传title title有默认内容
-(void)sureAlertWithTitle:(NSString *)title andMesg:(NSString *)msg
{
    if (title.length == 0 || !title) {
        title = KNSLocal(@"alertTitle");
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction=[UIAlertAction actionWithTitle:KNSLocal(@"alertOK") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertController addAction:alertAction];
        [self presentViewController:alertController animated:YES completion:nil];
    });
}

//一般不传title title有默认内容
-(void)systemAlertWithTitle:(NSString *)title
                 andMessage:(NSString *)message
              actionTextOne:(NSString *)actionTextOne
              actionTextTwo:(NSString *)actionTextTwo
             actionOneBlock:(void(^)())actionOneBlock
             actionTwoBlock:(void(^)())actionTwoBlock
{
    if (title.length == 0 || !title) {
        title = KNSLocal(@"alertTitle");
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        
        if (actionTextOne.length > 0 &&  actionTextOne) {
            UIAlertAction * actionOne =[UIAlertAction actionWithTitle:actionTextOne style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    actionOneBlock();
                });
            }];
            [alertController addAction:actionOne];
        }
        if (actionTextTwo.length > 0 && actionTextTwo) {
            UIAlertAction * actionTwo = [UIAlertAction actionWithTitle:actionTextTwo style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    actionTwoBlock();
                });
            }];
            [alertController addAction:actionTwo];
        }
        [self presentViewController:alertController animated:YES completion:nil];
    });
}

#pragma mark -  带输入框的弹框
-(void)systemAlertWithTitle:(NSString *)title
                 andMessage:(NSString *)message
                   needText:(BOOL)needText
                    content:(NSString *)content
                placeholder:(NSString *)placeholder
              needNumberPad:(BOOL)needNumberPad
              actionTextOne:(NSString *)actionTextOne
              actionTextTwo:(NSString *)actionTextTwo
             actionOneBlock:(void(^)())actionOneBlock
             actionTwoBlock:(void(^)(NSString * text))actionTwoBlock
{
    if (title.length == 0 || !title) {
        title = KNSLocal(@"alertTitle");
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        
        if (actionTextOne.length > 0 &&  actionTextOne) {
            UIAlertAction * actionOne =[UIAlertAction actionWithTitle:actionTextOne style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    actionOneBlock();
                });
            }];
            [alertController addAction:actionOne];
        }
        
        if (needText) {
            [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
                textField.placeholder = placeholder;
                textField.text = content;
                textField.clearButtonMode = UITextFieldViewModeWhileEditing;
                if (needNumberPad) {
                    textField.keyboardType = UIKeyboardTypeNumberPad;
                }
            }];
        }
        
        if (actionTextTwo.length > 0 && actionTextTwo) {
            UIAlertAction * actionTwo = [UIAlertAction actionWithTitle:actionTextTwo style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (needText) {
                    UITextField * inPutText = alertController.textFields.firstObject;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        actionTwoBlock(inPutText.text);
                    });
                }else{
                    actionTwoBlock(@"");
                }
            }];
            [alertController addAction:actionTwo];
        }
        
        
        [self presentViewController:alertController animated:YES completion:nil];
    });
}

-(void)showLoadingHudWithHText:(NSString *)text
{
    if (text.length == 0 || !text) {
        text = KNSLocal(@"loadding");
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = text;
    hud.labelFont = PFR_FONT(12);
}

-(void)hiddenLoadingHud
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    [[SDWebImageManager sharedManager] cancelAll];
    [[SDImageCache sharedImageCache] clearMemory];

}


-(BOOL)canProcessNext
{
    if ([[GooGleAdMobMgr shared] showInterstitialWithVC:self]) {
        return NO;
    }
    
    if ([[UserInfoManger shared] needGotoLoginLogic]) {
        return NO;
    }
    return YES;
}


#pragma mark - 关于自定义返回按钮后 解决滑动动画卡死的bug
/**
 * 禁用边缘返回
 */
-(void)forbiddenSideBack
{
    self.isCanUseSideBack = NO;
    //关闭ios右滑返回
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate=self;
    }
}

/*
 * 恢复边缘返回
 */
- (void)resetSideBack
{
    self.isCanUseSideBack = YES;
    //开启ios右滑返回
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer
{
    return self.isCanUseSideBack;
}



@end
